package com.iwayin.web.action.comment;

import java.util.List;

import com.iwayin.core.bo.comment.CommentBO;
import com.iwayin.core.dataobject.CommentDO;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.utils.exception.BOException;
import com.iwayin.web.action.BaseAction;
import com.iwayin.web.common.AjaxResult;
import com.iwayin.web.common.WebConstants;

public class CommentAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CommentDO comment;
	private CommentDO query;
	private List<CommentDO> comments;
	private AjaxResult ajaxResult;

	private CommentBO commentBO;

	/**
	 * @author ZhangLei 添加评论
	 * @return
	 */
	public String addComment() {
		UserDO loginUser = getLoginUser();
		if (loginUser == null) {
			ajaxResult = AjaxResult.errorResult("未登陆");
			ajaxResult.setResultCode(9999);
			return WebConstants.UNLOGIN_BACK_ACTION;
		}
		try {
			comment.setUserId(loginUser.getId());
			commentBO.addComment(comment);
			ajaxResult = AjaxResult.succResult();
		} catch (BOException e) {
			ajaxResult = AjaxResult.errorResult(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * @author ZhangLei
	 * 
	 *         删除评论
	 * @return
	 */
	public String deleteComment() {
		try {
			this.commentBO.deleteComment(comment);
			ajaxResult = AjaxResult.succResult();
		} catch (Exception e) {
			ajaxResult = AjaxResult.errorResult("删除评论失败!");
		}
		return SUCCESS;
	}

	/**
	 * @author ZhangLei 获取问题的所有评论
	 * @return
	 */
	public String findAskComments() {
		if (query == null) {
			query = new CommentDO();
		}
		query.setLoginUserId(this.getLoginUser().getId());
		comments = this.commentBO.findAskComments(query);
		return SUCCESS;
	}

	public CommentBO getCommentBO() {
		return commentBO;
	}

	public void setCommentBO(CommentBO commentBO) {
		this.commentBO = commentBO;
	}

	public CommentDO getComment() {
		return comment;
	}

	public void setComment(CommentDO comment) {
		this.comment = comment;
	}

	public AjaxResult getAjaxResult() {
		return ajaxResult;
	}

	public void setAjaxResult(AjaxResult ajaxResult) {
		this.ajaxResult = ajaxResult;
	}

	public CommentDO getQuery() {
		return query;
	}

	public void setQuery(CommentDO query) {
		this.query = query;
	}

	public List<CommentDO> getComments() {
		return comments;
	}

	public void setComments(List<CommentDO> comments) {
		this.comments = comments;
	}

}
